package com.happok.onlineclass.usermanagement.authentication.dto;

import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 接口分类
 * 功能说明
 *
 * @author : chenac
 * @module 项目名称/模块名          yapi 项目组/子项目
 * @date : 2021/6/28 12:05
 */
@Data
public class UserDetailsDTO implements UserDetails {
    /**
     * 用户id
     */
    private Integer id;
    /**
     * 用户昵称
     */
    private String nickName;
    /**
     * 密码
     */
    private String password;

    /**
     * 禁用启用，true 启用，false 禁用
     */
    private Boolean enable;

    /**
     * 培训机构用户权限
     */
    private Map<Integer, List<GrantedAuthority>> trainOrgAuthorityMap;

    /**
     * 企业用户用户权限
     */
    private Map<Integer, List<GrantedAuthority>> enterpriseAuthorityMap;

    /**
     * 平台用户用户权限
     */
    private List<GrantedAuthority> platformAuthoritys;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authorities = new ArrayList<>();
        authorities.addAll(platformAuthoritys);
        for (Map.Entry<Integer, List<GrantedAuthority>> entry : trainOrgAuthorityMap.entrySet()){
            authorities.addAll(entry.getValue());
        }
        for (Map.Entry<Integer, List<GrantedAuthority>> entry : enterpriseAuthorityMap.entrySet()){
            authorities.addAll(entry.getValue());
        }
        return authorities;
    }

    /**
     * 获取培训机构的权限，没有返回空集合
     *
     * @param orgId
     * @return
     */
    public Collection<? extends GrantedAuthority> getTrainOrgAuthorities(Integer orgId) {
        return trainOrgAuthorityMap.get(orgId);
    }

    /**
     * 获取企业用户的权限，没有返回空集合
     *
     * @param enterpriseId
     * @return
     */
    public Collection<? extends GrantedAuthority> getEnterprise(Integer enterpriseId) {
        return enterpriseAuthorityMap.get(enterpriseId);
    }


    /**
     * 获取平台管理的权限，没有返回空集合
     *
     * @return
     */
    public Collection<? extends GrantedAuthority> getPlatAuthorities() {
        return platformAuthoritys;
    }

    @Override
    public String getUsername() {
        return this.id.toString();
    }

    @Override
    public boolean isAccountNonExpired() {
        // 指定账户是否未过期.
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        // 指定账户是否未锁定.
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        // 指定密码是否未过期.
        return true;
    }

    @Override
    public boolean isEnabled() {
        // 指定用户是否已启用, 禁用的用户无法进行身份验证.
        return this.enable;
    }
}